<template>
  <span ref="scrollIntoView" id="tem1-scroll-box-top"></span>
  <div class="btm-arrow">
    <a class="arrow-item" target="_self" href="javascript:void(0)" @click="stepExhibit(1)">
      <div class="arrow-img">
        <arrowLeft />
      </div>
      <span
        :style="{
          fontFamily: store.styleInfo.fontFamily,
          fontSize: '16px',
          color: store.styleInfo.shmentColor,
        }"
        >上一个展品</span
      >
    </a>
    <a class="arrow-item" target="_self" href="javascript:void(0)" @click="stepExhibit(2)">
      <span
        :style="{
          fontFamily: store.styleInfo.fontFamily,
          fontSize: '16px',
          color: store.styleInfo.shmentColor,
        }"
        >下一个展品</span
      >
      <div class="arrow-img">
        <arrowRight />
      </div>
    </a>
  </div>
  <div class="heard-box" v-if="info">
    <div class="heard-left">
      <div class="left-top">
        <t-image
          v-if="imgUrl"
          class="left-top-img"
          fit="contain"
          position="center"
          :src="$filters.getLinkUrl(imgUrl + '?imageView2/2/w/680/format/webp')"
          alt=""
        />
        <div class="zoomIn" @click="open">
          <t-icon name="zoom-in" />
        </div>
      </div>
    </div>
    <div class="heard-right">
      <div class="right-item-level">
        <span>{{ info.name }}</span>
      </div>
      <div>
        <div class="right-item">
          <span class="item-title1">展品类型</span><span class="item-title2">{{ info.typeValue }}</span>
        </div>
        <div class="right-item">
          <span class="item-title1">展品来源</span
          ><span class="item-title2">{{ info.sourceValue ? info.sourceValue : '--' }}</span>
        </div>
        <div class="right-item">
          <span class="item-title1">所属年代</span
          ><span class="item-title2">{{ info.concreteTimes ? info.concreteTimes : info.ageValue || '-' }}</span>
        </div>
        <div class="right-item">
          <span class="item-title1">收藏单位</span
          ><span class="item-title2"> {{ info.location ? info.location : info.companyName || '-' }}</span>
        </div>
        <div class="right-item">
          <span class="item-title1">展品质地</span
          ><span class="item-title2">{{ info.texture ? info.texture : '--' }}</span>
        </div>
        <div class="right-item">
          <span class="item-title1">展品尺寸</span><span class="item-title2">{{ info.size ? info.size : '--' }}</span>
        </div>
        <div class="left-btm" v-if="info.images?.length > 0">
          <!-- <div class="left-btm-chevron" @click="putCruuent(1)">
          <t-icon name="chevron-left" color="#C0C4CC" size="30"></t-icon>
        </div> -->
          <div class="item-title1">展品图片</div>
          <div class="left-content" id="scroll-by-box">
            <div
              @click="lookIndex(index)"
              class="scroll-item"
              v-for="(item, index) in info.images"
              :key="index"
              :id="`scroll-child-${index}`"
              :class="{ 'img-active': cruuent == index }"
            >
              <div class="img-shadow">
                <span v-if="item.type == 1">正立面</span>
                <span v-else-if="item.type == 2">侧立面</span>
                <span v-else-if="item.type == 3">顶面</span>
                <span v-else-if="item.type == 4">底面</span>
                <span v-else-if="item.type == 5">侧面</span>
                <span v-else>未设置</span>
              </div>
              <t-image
                class="scroll-item-img"
                :src="$filters.getLinkUrl(item.data + '?imageView2/1/w/80/h/80/format/webp')"
                alt=""
              />
            </div>
          </div>
          <!-- <div class="left-btm-chevron" @click="putCruuent(2)">
          <t-icon name="chevron-right" color="#C0C4CC" size="30"></t-icon>
        </div> -->
        </div>
      </div>

      <div class="bottom-btn-box">
        <div class="btn-box">
          <div class="add-btn1" @click="emitAddInfo">
            <t-badge v-if="countNum" dot :count="countNum"
              ><i
                class="iconfonts"
                :style="{
                  fontSize: '24px',
                }"
              >
                &#xe680;</i
              ></t-badge
            >
            <i
              v-else
              class="iconfonts"
              :style="{
                fontSize: '24px',
              }"
            >
              &#xe680;</i
            >
            对比
          </div>
          <div>
            <t-popup content="" placement="bottom">
              <template #content>
                <shear @shearType="shearType" />
              </template>
              <div class="add-btn1">
                <t-icon size="22px" name="share"></t-icon>
                <span>分享</span>
              </div>
            </t-popup>
          </div>
          <div class="add-btn1" @click="postattention" v-if="info.openStatus === 1">
            <t-icon
              :name="!info.isFollow ? 'star' : 'star-filled'"
              :style="{
                'margin-right': '0px',
                color: info.isFollow ? '#fec53d' : 'rgba(0, 0, 0, .8)',
                'font-size': '24px',
              }"
            ></t-icon>
            <span>{{ info.followTotal }}</span>
          </div>
        </div>

        <div
          v-if="info.openStatus === 1"
          @click="addId"
          class="exhibit"
          :class="{ cursorAA: isInformation }"
          :style="{
            backgroundColor: store.styleInfo.themeColor,
            justifyContent: info.openStatus === 1 ? 'center' : 'left',
          }"
        >
          <i
            class="iconfonts"
            :style="{
              fontSize: '16px',
            }"
          >
            &#xe692;</i
          >
          添加至展览
        </div>
      </div>

      <t-image-viewer :v-model:index="viewerIndex" v-model:visible="visible" :images="urlList"> </t-image-viewer>
    </div>
    <div class="select-box">
      <t-dialog
        header="添加展品至展览"
        :visible="visibleStep"
        :confirm-on-enter="true"
        :on-cancel="closetables"
        :on-close="closetables"
        :cancelBtn="null"
        :confirmBtn="null"
      >
        <template #body>
          <t-row :gutter="16" style="width: 100%">
            <t-col :span="12">
              <div class="hw-dialog-box">
                <span class="titles">请选择展览:</span>
                <div class="hw-dialog-box-select">
                  <t-select v-model="projectId" placeholder="请选择展览">
                    <t-option
                      :key="item.projectId"
                      :label="item.name"
                      :value="item.projectId"
                      v-for="item in selectList"
                    />
                  </t-select>
                </div>
              </div>
            </t-col>
          </t-row>
        </template>
        <template #footer>
          <t-button @click="visibleStep = false" theme="default" variant="outline">取消</t-button>
          <t-button @click="okStep">确定</t-button>
        </template>
      </t-dialog>
    </div>
  </div>
  <div v-if="info.description" class="describe-box">
    <div class="dot">
      <dotIcon :ischange="1" fontsize="20px" />
      <p class="describe-title">展品描述</p>
      <dotIcon :ischange="1" fontsize="20px" />
    </div>
    <div class="describe-text" v-html="info.description"></div>
  </div>
  <exhibitVue :info="info" @emitchangeExhi="emitchangeExhi" :showpieceid="showpieceid" />
  <messageModel
    submitText="前往"
    title="提示"
    :visible="visibleMessage1"
    problem="您暂未登录"
    message="是否前往登录？"
    @closeClick="closeClick"
    @submitClick="pathSubmitClick"
  />
  <NavVue :flag="true" :name="info.name"></NavVue>
</template>

<script setup lang="ts">
// @ts-nocheck
import { doorUrl } from '@/utils/minxin';
import { urls } from '@/utils/request/upload';
import { ref, watch } from 'vue';
import { MessagePlugin } from 'tdesign-vue-next';
import { useProjectStore, getExhibitStore } from '@/store';
import { followSet } from '@/api/interact/interact';
import arrowLeft from './arrowLeft.vue';
import arrowRight from './arrowRight.vue';
import dotIcon from './dotIcon.vue';
import { useRoute } from 'vue-router';
import { exhibitShowpiece } from '@/api/templates/templates';
import exhibitVue from '../exhibitsInfo.vue';
import messageModel from '@/components/messageModel/index.vue';
import { getHeardToken } from '@/api/utils';
import { projectLists } from '@/api/project/project';
import { addExhibits } from '@/api/desgin/desgin';
import shear from '@/components/shear/shear.vue';
import { toQQzone, toQQ, toWeibo, toWechat } from '@/utils/shear';
import NavVue from './nav.vue';
const storeExhibit = getExhibitStore();
const countNum = ref(false);
const route = useRoute();
const store = useProjectStore();
const imgUrl = ref('');
const visible = ref(false);
const viewerIndex = ref(0);
const urlList = ref([]);
const selectList = ref(null);
const info = ref({});
const cruuent = ref(0);
const loading = ref(false);
const visibleStep = ref(false);
const projectId = ref(null);
const scrollIntoView = ref(null);
const visibleMessage1 = ref(false);
// const emit = defineEmits(['changeattention']);
const getImgurl = (e: any) => {
  if (e.indexOf('https://') !== -1) {
    return e;
  } else {
    return urls + e;
  }
};
const init = async () => {
  try {
    console.log(showpieceid.value);

    let { data } = await exhibitShowpiece(moduleId.value, showpieceid.value);
    info.value = data;
    urlList.value = [];
    console.log(info.value.coverUrl);

    urlList.value.push(getImgurl(info.value.coverUrl));
    // if (info.value.images.length > 0) {
    //   imgUrl.value = info.value.images[0].data;
    //   info.value.images.forEach((element) => {
    //     urlList.value.push(getImgurl(element.data));
    //   });
    // } else {
    imgUrl.value = info.value.coverUrl;
    // }
    console.log('info.id', urlList.value);
    // initInfo();
  } catch {}
  find();
};
const closeClick = () => {
  //取消
  visibleMessage1.value = false;
};

const pathSubmitClick = () => {
  //确认登陆

  visibleMessage1.value = false;
  let url = window.location.href;
  let redirectUrl = doorUrl + 'login?redirect=' + url;
  // window.location.href = redirectUrl;
  outLogin(redirectUrl);
};
const moduleId = ref(0);
const showpieceid = ref(0);
moduleId.value = route.query.moduleId;
showpieceid.value = Number(route.query.id);
init();
const open = () => {
  visible.value = true;
};

const putCruuent = (type: number, index: Number) => {
  if (type == 1 && cruuent.value !== 0) {
    cruuent.value = cruuent.value - 1;
  } else if (type === 2 && cruuent.value !== info.value.images.length - 1) {
    cruuent.value = cruuent.value + 1;
  }
  console.log(`scroll-child-${cruuent.value}`);
  let ele = document.getElementById(`scroll-child-${cruuent.value}`);
  ele.scrollIntoView({ behavior: 'smooth', block: 'center' });
  imgUrl.value = info.value.images[cruuent.value].data;
  // ele.scrollIntoView();
};
const lookIndex = (e: number) => {
  cruuent.value = e;
  let ele = document.getElementById(`scroll-child-${cruuent.value}`);
  ele.scrollIntoView({ behavior: 'smooth', block: 'center' });
  imgUrl.value = info.value.images[cruuent.value].data;
};
watch(
  () => info.value.id,
  () => {
    // initInfo();
  },
  { deep: true },
);
watch(
  () => showpieceid.value,
  () => {
    init();
  },
  { deep: true },
);

const find = () => {
  console.log('route', storeExhibit.ExhibitList);

  const targetExhibit = storeExhibit.ExhibitList.filter((exhibit) => exhibit.id == showpieceid.value);
  if (targetExhibit.length > 0) {
    countNum.value = 1;
  } else {
    countNum.value = 0;
  }
};

watch(
  () => storeExhibit.ExhibitList,
  () => {
    console.log('storeExhibit', storeExhibit.ExhibitList);
    find();
  },
  { immediate: true, deep: true },
);

// 添加关注事件
const postattention = async () => {
  try {
    await followSet({ refType: 4, refId: info.value.id });
    if (info.value.isFollow) {
      MessagePlugin.success('关注成功');
    } else {
      MessagePlugin.warning('取消关注');
    }
    info.value.isFollow = !info.value.isFollow;
  } catch {
    console.log('失败');
  }
};
const stepExhibit = (type: number) => {
  console.log(type);

  if (type === 1) {
    if (info.value.prev) {
      showpieceid.value = info.value.prev.value;

      document.documentElement.scrollTop = 0;
    } else {
      MessagePlugin.error('暂无上一个展品');
      return;
    }
  } else {
    if (info.value.next) {
      showpieceid.value = info.value.next.value;
      console.log(info.value.next);

      document.documentElement.scrollTop = 0;
    } else {
      MessagePlugin.error('暂无下一个展品');
      return;
    }
  }
  init();
};
const selectInit = async (type: number) => {
  //获取账号项目
  try {
    let res = await projectLists({
      type: 0,
      value: '',
      PageNo: 1,
      PageSize: 99,
    });
    selectList.value = res.data.data;
    visibleStep.value = true;
  } catch (err) {
    console.log(err);
  }
};
const addId = () => {
  let token = getHeardToken();
  if (!token) {
    visibleMessage1.value = true;
    return;
  }
  selectInit();
};
const okStep = async () => {
  //添加项目
  try {
    if (loading.value) {
      return;
    }
    if (projectId.value) {
      loading.value = true;
      let res = await addExhibits(projectId.value, 0, [info.value.id]);
      console.log(res.data);

      if (res.data) {
        MessagePlugin.success('添加成功');
        visibleStep.value = false;
      }

      loading.value = false;
    } else {
      MessagePlugin.success('请选择展览');
      return;
    }
  } catch {
    loading.value = false;
  }
};
const shearType = (e: number) => {
  //分享
  let url = window.location.href;
  let title = info.value.name;
  switch (e) {
    case 1:
      toQQzone(url, title);
      break;
    case 2:
      toQQ(url, title);
      break;
    case 3:
      toWeibo(url, title);
      break;
    default:
      toWechat(url, title);
      break;
  }
};
const top = () => {
  scrollIntoView.value.scrollIntoView({ behavior: 'smooth' });
  // window.scrollTo({
  //   top: 0,
  //   behavior: 'smooth', // 设置滚动的动画效果
  // });
};
const emitAddInfo = () => {
  storeExhibit.pushExhibitInfo(info.value);
};
const emitchangeExhi = (info: any) => {
  console.log(info);

  showpieceid.value = info;
  init();
};
</script>

<style lang="less" scoped>
.dot {
  height: 30px;
  // line-height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 50px;
  font-size: 20px;
  font-family: SourceHanSerifCN-Bold, SourceHanSerifCN;
  > p {
    padding: 0 5px;
  }
}
.describe-box {
  padding: 0 100px;
  padding-top: 80px;
  padding-bottom: 50px;
  margin-top: 80px;
  background: #f0e9e1;
  .describe-title {
    text-align: center;
    font-size: 22px;
    font-family: SourceHanSerifCN-Bold, SourceHanSerifCN;
    font-weight: bold;
    color: #333333;
    line-height: 34px;
  }
  .describe-text {
    width: 980px;
    margin: 0 auto;
    font-size: 17px;
    font-family: SourceHanSerifCN-Medium, SourceHanSerifCN;
    font-weight: 500;
    color: #333333;
    line-height: 32px;
    margin-bottom: 30px;
  }
}
.copyright-box {
  //   padding: 50px 130px;
}

.t-row {
  margin: 0;
}

.heard-box {
  display: flex;
  width: 1180px;
  margin: 0 auto;
  padding: 24px 0px;
}

.heard-left {
  width: 55%;
  display: flex;
  flex-direction: column;
}

.left-top {
  width: 100%;
  height: 580px;
  position: relative;

  .left-top-img {
    width: 100%;
    height: 580px;
  }
}

.heard-right {
  // flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  margin-left: 72px;
}

.right-item {
  display: flex;
  align-items: center;
  width: 100%;
  margin-bottom: 16px;
}

.item-title1 {
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #aaaaaa;
  line-height: 32px;
  width: 56px;
}

.item-title2 {
  display: flex;
  flex: 1;
  font-size: 17px;
  font-family: SourceHanSerifCN-Medium, SourceHanSerifCN;
  font-weight: 500;
  color: #333333;
  line-height: 32px;
  margin-left: 16px;
}

.right-item-level {
  font-size: 28px;
  font-family: SourceHanSerifCN-Bold, SourceHanSerifCN;
  font-weight: bold;
  color: #333333;
  line-height: 42px;

  img {
    width: 72px;
    height: 20px;
    margin-left: 18px;
  }

  margin-bottom: 32px;
}

.btn-box {
  display: flex;
  margin-top: 20px;
  margin-bottom: 19px;
}

.add-btn {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50%;
  height: 56px;
  background: #cfbda8;
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 24px;

  img {
    width: 40px;
    height: 40px;
    // margin-left: 6px;
  }
}

.add-btn1 {
  cursor: pointer;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  width: 56px;
  height: 56px;
  font-size: 14px;
}
.add-btn1:hover {
  background: #f0e9e1;
}
.left-btm {
  display: flex;
  margin-top: 12px;
}

.left-btm-chevron {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 88px;
  background: rgba(0, 0, 0, 0.03);
  border-radius: 3px;
}

.left-content {
  display: flex;
  flex: 1;
  height: 88px;
  margin: 0 12px;
  overflow: hidden;
  overflow-y: auto;
  // overflow-y: hidden;
}

.scroll-item {
  cursor: pointer;
  position: relative;
  width: 88px;
  height: 88px;
  padding: 1px;
  border: 1px solid transparent;
  margin-right: 12px;

  .scroll-item-img {
    width: 84px;
    height: 84px;
  }
}

.img-shadow {
  display: none;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  background: rgba(0, 0, 0, 0.4);
  font-size: 16px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 24px;
}

.scroll-item:hover {
  .img-shadow {
    display: flex;
  }
}

.img-active {
  border: 1px solid #c7ab8b;
}

.btm-arrow {
  width: 1180px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: auto;

  padding-top: 28px;
}

.arrow-item {
  display: flex;
  align-items: center;

  span {
    font-size: 17px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    line-height: 32px;
    margin: 0 8px;
  }
}

.zoomIn {
  width: 40px;
  height: 40px;
  cursor: pointer;
  position: absolute;
  top: 20px;
  right: 24px;
  z-index: 0;
  font-size: 20px;
  color: #fff;
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
  transition: all 0.3s;
  > .t-icon {
    font-size: 24px;
    position: relative;
    top: 50%;
    left: 60%;
    transform: translate(-50%, -50%);
  }
}
.zoomIn:hover {
  opacity: 70%;
}
.t-image__wrapper {
  background: #f0e9e1;
  z-index: 0;
}

.exhibit {
  width: 336px;
  height: 56px;
  text-align: center;
  color: #fff;
  line-height: 56px;
  cursor: pointer;
  margin-left: 24px;
}
.exhibit:hover {
  opacity: 0.7;
}
.bottom-btn-box {
  width: 100%;
  height: 56px;
  display: flex;
  // justify-content: center;
  align-items: center;
}
</style>
